package lazason.namecounter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class NameCounter {
	private static final String root = "/Users/lazason/Documents/";
	private static final String JIN_FILE = "jin.txt";
	private static final String MU_FILE = "mu.txt";
	private static final String SHUI_FILE = "shui.txt";
	private static final String HUO_FILE = "huo.txt";
	private static final String HUO_OUTPUT_FILE = "huo_out.txt";
	private static final String TU_FILE = "tu.txt";
	private static final String WUXING_OUTPUT_FILE = "wuxing_out.txt";
	private static final String WUXING_FILE = "wuxing.txt";
	private static String readFromFile = null;
	private static String writeToFile = null;
	private static ArrayList<String> huo = new ArrayList<String>();
	private static ArrayList<String> youyong = new ArrayList<String>();
	static FileWriter writer;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		long start = System.currentTimeMillis();
		readFromFile = root + WUXING_FILE;
		writeToFile = root + WUXING_OUTPUT_FILE;
		int counter = 0;
		String nameCounter = null;
		try {
			readFromFile(huo);
			writer = new FileWriter(writeToFile, true);
			for (String string : huo) {
				nameCounter = getNameCounter("李" + string);
				if (!nameCounter.equals("0")) {
					youyong.add(string);
					writeToFile("李" + string + " " + nameCounter);
					writeToFile("\n");
					counter++;
				}
				
				Thread.sleep(10);
			}
//			for (String string1 : youyong) {
//				for (String string2 : youyong) {
//				nameCounter = getNameCounter("李" + string1+string2);
//				if (!nameCounter.equals("0")) {
//					 
//					writeToFile("李" + string1+string2 + " " + nameCounter);
//					writeToFile("\n");
//					counter++;
//				}
//				
//				Thread.sleep(10);
//				}
//			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}// 关闭输出流，施放资源
			long end = System.currentTimeMillis();
			System.out.println("get " + counter + " of " + huo.size() + " in "
					+ (end - start) + " ms");
		}

	}

	private static void readFromFile(ArrayList<String> list)
			throws FileNotFoundException, IOException {
		File file = new File(readFromFile);// 指定要读取的文件
		FileReader reader = new FileReader(file);// 获取该文件的输入流

		String str = "";// 用来将每次读取到的字符拼接，当然使用StringBuffer类更好
		int n = 0;// 每次读取到的字符长度
		String regEx = "[0-9]";
		Pattern p = Pattern.compile(regEx);

		while ((n = reader.read()) != -1) {

			str = String.valueOf((char) n);
			str = str.trim();
			if (str.trim() != null) {

				Matcher m = p.matcher(str);
				str = m.replaceAll("").trim();
				if (!str.equals("")) {
					// System.out.println(str);
					list.add(str);
				}
			}
		}
		reader.close();// 关闭输入流，释放连接

	}

	private static void writeToFile(String content) throws IOException {
		File file = new File(writeToFile);// 要写入的文本文件
		if (!file.exists()) {// 如果文件不存在，则创建该文件
			file.createNewFile();
		}

		writer.write(content);// 写内容
		writer.flush();// 清空缓冲区，立即将输出流里的内容写到文件里

	}

	private static String getNameCounter(String name) {
		String url = null;
		try {
			url = new String("http://name.renren.com/xingming/search?q="
					+ URLEncoder.encode(name, "utf8"));
			// Log.d("xingming",url);
			System.out.println(url);
			Document doc = Jsoup.connect(url).get();
			Elements namesakers = doc.getElementsByTag("h2");

			String regEx = "[^0-9]";
			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(namesakers.html());
			return m.replaceAll("").trim();
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
}
